package com.czn.service.impl;

import com.czn.dao.DoctorDao;
import com.czn.entity.Doctor;
import com.czn.service.IDoctorService;
import com.czn.vo.ResponseResult;
import org.beetl.sql.core.engine.PageQuery;
import org.beetl.sql.core.query.LambdaQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @author ChenZeNan
 * @create 2021-04-15 19:10
 */
@Service
public class DoctorServiceImpl implements IDoctorService {
    @Autowired
    private DoctorDao doctorDao;

    /**
     * 查询所有的医生基本信息
     *
     * @return
     */
    @Override
    public List<Doctor> findAllDoctor() {
        List<Doctor> all = doctorDao.all();
        return all;
    }

    /**
     * 分页查询医生
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Override
    public PageQuery<Doctor> findDoctorList(Integer pageNo, Integer pageSize, String name, String certId) {
        LambdaQuery<Doctor> query = doctorDao.createLambdaQuery();
        if (!StringUtils.isEmpty(name)) {
            query.andLike(Doctor::getName, "%" + name + "%");
        }
        if (!StringUtils.isEmpty(certId)) {
            query.andEq(Doctor::getCertId, certId);
        }
        if (pageNo > 0 && pageSize > 0) {
            PageQuery<Doctor> page = query.desc(Doctor::getId).page(pageNo, pageSize);
            return page;
        }
        return null;
    }

    /**
     * 新增医生
     *
     * @param doctor
     * @return
     */
    @Override
    public ResponseResult addDoctor(Doctor doctor) {
        ResponseResult result = new ResponseResult();
        // 根据身份证查询数据库中是否已有该对象了
        LambdaQuery<Doctor> query = doctorDao.createLambdaQuery();
        query.andEq(Doctor::getCertId, doctor.getCertId());
        Doctor sysDoctor = query.single();
        if (sysDoctor == null) { // 数据库中没有该对象，直接插入
            doctorDao.insert(doctor);
            result.setCode("300");
            result.setMessage("信息添加成功！");
            return result;
        }
        // 数据库中已有该对象了
        result.setCode("301");
        result.setMessage("该身份证已经被注册~");
        return result;
    }

    /**
     * 医生修改
     *
     * @param doctor
     * @return
     */
    @Override
    public ResponseResult editDoctor(Doctor doctor) {
        ResponseResult result = new ResponseResult();
        int rows = doctorDao.updateById(doctor);
        if (rows > 0) {
            result.setCode("500");
            result.setMessage("信息更新成功！");
            return result;
        }
        result.setCode("501");
        result.setMessage("信息更新失败！");
        return result;
    }


    /**
     * 根据记录id查询医生
     *
     * @param id
     * @return
     */
    @Override
    public Doctor findDoctorById(Integer id) {
        Doctor doctor = doctorDao.single(id);
        return doctor;
    }

    /**
     * 根据session中的user_id查询医生
     *
     * @param doctorId
     * @return
     */
    @Override
    public Doctor findDoctorByUserId(Integer doctorId) {
        LambdaQuery<Doctor> query = doctorDao.createLambdaQuery();
        if (doctorId != null) {
            query.andEq("user_id", doctorId);
        }
        return query.single();
    }

    /**
     * 根据记录id进行删除
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult deleteDoctorById(Integer id) {
        ResponseResult result = new ResponseResult();
        int rows = doctorDao.deleteById(id);
        if (rows > 0) {
            result.setCode("400");
            result.setMessage("删除成功！");
            return result;
        }
        result.setCode("401");
        result.setMessage("删除失败！");
        return result;
    }

    /**
     * 根据科室查询医生
     *
     * @param department
     * @return
     */
    @Override
    public List<Doctor> findDoctorListByDepartment(String department) {
        LambdaQuery<Doctor> query = doctorDao.createLambdaQuery();
        query.andEq(Doctor::getDepartment, department);
        List<Doctor> doctors = query.select();
        return doctors;
    }


}
